/*
 * task_wrapper.h
 *
 *  Created on: 2011-5-15
 *      Author: chenl
 */

#ifndef TASK_WRAPPER_H_
#define TASK_WRAPPER_H_

#include "os/sched.h"
#include "processmgt.h"

class CTask
{
public:
	CTask()
	{
//		InitTask();
	}
	~CTask()
	{

	}
public:
	static CTask& InitTask()
	{
		static CTask initTask;
		static bool bInited = false;
		if(!bInited)
		{
			INIT_TASK(initTask);
			++pidmapped;
			bInited = true;
		}
		return initTask;
	}

	int32_t NewTask(CTask** ppNewTask)
	{
		*ppNewTask = new CTask(*this);
		(*ppNewTask)->task.pid = pidmapped;
		++pidmapped;
		return GET_PROCESSMGT_INSTANCE().NewTask(*this, **ppNewTask);
	}

	int32_t DestoryTask()
	{
		int32_t ret = GET_PROCESSMGT_INSTANCE().DeleteTask(*this);
		delete this;
		return ret;
	}

	bool operator==(const CTask& t) const
	{
		return t.task.pid == task.pid;
	}

	void UpdateTaskTimes()
	{
		if (CTask::InitTask() == *this)
		{
			return;
		}

		if (0 >= --task.counter)
		{
			task.counter = 0;
			task.need_resched = 1;
		}
	}

	bool SignalPending() const
	{
		return 0 == task.sigpending ? false : true;
	}

public:
	static pid_t pidmapped;
	struct task_struct task;

};

#endif /* TASK_WRAPPER_H_ */
